/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.pictual.controller.mainPackage;

import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;
import java.net.*;
//import java.System.out.*;

/**
 *
 *
 */
public class EnglishWikiDicParser extends WikiDicParser {

    private static List<String> _types = Arrays.asList(
            "Noun",
            "Verb",
            "Adjective",
            "Verb_form",
            "Adverb",
            "Preposition",
            "Particle",
            "Article",
            "Pronoun",
            "Proper_noun");

    protected boolean isLang(String language) {
        return Language.isLang(0, language);
    }

    protected boolean IsType(String word) {
        return _types.contains(word);
    }

    public String WikiLang(int i) {
    	System.out.println("WIKILANG:"+i);
        return "English";
    }

    public void extractAudio(String line, DefinitionList listdef){
        line = line.substring(line.indexOf("<table"));
        //String id = line.substring(line.indexOf("Audio"), line.indexOf("</td>"));
        String extension;
        int index = line.indexOf(".ogg");
        if(index < 0){
            index = line.indexOf(".OGG");
            extension = ".OGG";
        } else{
            extension = ".ogg";
        }
        String link = line.substring(line.indexOf("http"), index);
        link = "href=\""+link+extension+"\" class=\"internal\" title=\"Audio"+listdef.getAudios().size()+"\" target='audioZone'";
        System.out.println("AUDIO: "+link);
        listdef.addAudio(link);
    }

    public void extractPronunciation(String line, DefinitionList listdef){
        String tmpPronunciation = "<br><br>";
        int index1 = line.indexOf("<li>");
        int index2 = line.indexOf("</li>");
        while(index1 >=0 && index2 >=0){
            String element = line.substring(index1, index2);
            if(element.contains("<table")){
                extractAudio(element, listdef);
            }
            if(element.contains("IPA") && tmpPronunciation.length() < 9){
                int i1, i2;
                String ref = element.substring(element.indexOf("IPA\">")+5);
                ref = ref.substring(0, ref.indexOf("<"));
                if(ref.indexOf("/") >= 0){
                    ref = ref.substring(ref.indexOf("/")+1);
                    ref = ref.substring(0, ref.indexOf("/"));
                }
                tmpPronunciation += ref;
                //System.out.println("Ex:"+ref);
                listdef.setPronunciation(tmpPronunciation);
            }
                line = line.substring(index2+5);
                index1 = line.indexOf("<li>");
                /*if(index1 >=0){
                    line = line.substring(index1);
                }*/
                index2 = line.indexOf("</li>");
                if(index1 > index2){
                    line = line.substring(index2+5);
                    index1 = line.indexOf("<li>");
                    index2 = line.indexOf("</li>");
                }
        }
    }

    public void extractDefinitions(String line, DefinitionList listdef){
        int index1 = line.indexOf("<li>");
        int index2 = line.indexOf("</li>")+5;
        while(index1 >=0 && index2 >=0){
            String element = line.substring(index1, index2);
            if(element.contains("<ul>")){
                String definition = line.substring(index1, line.indexOf("<ul>"));
                index1 = line.indexOf("<ul>");
                index2 = line.indexOf("</ul>")+10;
                element = line.substring(index1, index2);
                int in1 = element.indexOf("<li>");
                int in2 = element.indexOf("</li>");// habria que avanzar y mirar el resto
                if(element.contains("<dl>")){
                    String element2 = element.substring(element.indexOf("<dl>"), element.indexOf("</dl>"));
                    element = element.replace(element2, "");
                    element = element.replace("</dl>", "");
                    int i1, i2;
                    i1 = element2.indexOf("<dd>");
                    i2 = element2.indexOf("</dd>");
                    while(i1 >=0 && i2 >= 0){
                        String example = element2.substring(i1+4, i2);
                        example = example.replaceAll("<dl>", "");
                        example = example.replaceAll("</dl>", "");
                        example = example.replaceAll("<dd>", "");
                        example = example.replaceAll("</dd>", "");
                        listdef.addExample(example);
                        element2 = element2.substring(i2+5);
                        i1 = element2.indexOf("<dd>");
                        i2 = element2.indexOf("</dd>");
                    }
                } else{
                    definition += element;
                }
                definition = definition.replaceAll("<li>", "");
                definition = definition.replaceAll("</li>", "");
                listdef.addDefinition(definition);
            } else if(element.contains("<dl>")){
                String element2 = element.substring(element.indexOf("<dl>"), element.indexOf("</dl>"));
                element = element.replace(element2, "");
                element = element.replace("</dl>", "");                
                int i1, i2;
                i1 = element2.indexOf("<dd>");
                i2 = element2.indexOf("</dd>");
                while(i1 >=0 && i2 >= 0){
                    String example = element2.substring(i1+4, i2);
                    listdef.addExample(example);
                    element2 = element2.substring(i2+5);
                    i1 = element2.indexOf("<dd>");
                    i2 = element2.indexOf("</dd>");
                }
                listdef.addDefinition(element);
            } else{
                listdef.addDefinition(element);
            }
            line = line.substring(index2);
            index1 = line.indexOf("<li>");
            index2 = line.indexOf("</li>")+5;
         }
    }

    public void extractGender(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("class=\"gender"));
            String gender = "";
            char option = line2.charAt(14);
            switch(option){
                case 'm':
                    gender = "der";
                    break;
                case 'f':
                    gender = "die";
                    break;
                case 'n':
                    gender = "das";
                    break;
            }
            listdef.setGender(gender);
        }catch(Exception e){
            System.out.println(e.getMessage());
            if(line.contains("<i>m</i>")){
                listdef.setGender("der");
            }
            if(line.contains("<i>f</i>")){
                listdef.setGender("die");
            }
            if(line.contains("<i>n</i>")){
                listdef.setGender("das");
            }
        }
    }

    public void extractGenderFrench(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("class=\"gender"));
            String gender = "";
            char option = line2.charAt(14);
            switch(option){
                case 'm':
                    gender = "le";
                    break;
                case 'f':
                    gender = "la";
                    break;
            }
            listdef.setGender(gender);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    public void extractGenderSpanish(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("class=\"gender"));
            String gender = "";
            char option = line2.charAt(14);
            switch(option){
                case 'm':
                    gender = "el";
                    break;
                case 'f':
                    gender = "la";
                    break;
            }
            listdef.setGender(gender);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    public void extractPlural(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("class=\"form-of plural"));
            line2 = line2.substring(0, line2.indexOf(")"));
            String plural = "";
            if(line2.indexOf("title=") < 0){
                plural = line2.substring(line2.indexOf("<b>")+3, line2.indexOf("</b>"));
                while(plural.indexOf("<") >= 0){
                    String ref = plural.substring(plural.indexOf("<"), plural.indexOf(">")+1);
                    plural = plural.replace(ref, "");
                }
            } else{
                plural = line2.substring(line2.indexOf("title="));
                plural = plural.substring(7, plural.indexOf(">")-1);
            }
            listdef.setPlural(plural);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    protected void extractEnglish(String line, DefinitionList listdef){
        if(line.contains("id=\"Pronunciation")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("id=\"Pronunciation"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation(line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains("id=\""+_types.get(i))){
                index += line2.indexOf("id=\""+_types.get(i))+1;
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verb"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                line2 = line2.substring(line2.indexOf("<ol>"), line2.indexOf("</ol>")+5);
                extractDefinitions(line2, listdef);
            }
        }        
    }

    protected void extractSpanish(String line, DefinitionList listdef){
        if(line.contains("id=\"Pronunciation")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("id=\"Pronunciation"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation(line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains("id=\""+_types.get(i))){
                index += line2.indexOf("id=\""+_types.get(i));
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verb"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                if(line2.contains("id=\"Noun")){
                    extractGenderSpanish(line2, listdef);
                }
                line2 = line2.substring(line2.indexOf("<ol>"), line2.indexOf("</ol>")+5);
                extractDefinitions(line2, listdef);
            }
        }
        if(listdef.isPlural()){
            if(listdef.getGender().equals("la")){
                listdef.setGender("las");
            } else{
                listdef.setGender("los");
            }
        }
    }

    protected void extractGerman(String line, DefinitionList listdef){
        if(line.contains("id=\"Pronunciation")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("id=\"Pronunciation"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation(line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains("id=\""+_types.get(i))){
                index += line2.indexOf("id=\""+_types.get(i));
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verb"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
           for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                if(line2.contains("id=\"Noun")){
                    extractGender(line2, listdef);
                    extractPlural(line2, listdef);
                }
                line2 = line2.substring(line2.indexOf("<ol>"), line2.indexOf("</ol>")+5);
                extractDefinitions(line2, listdef);
            }
        }
        if(listdef.isPlural()){
            listdef.setGender("pl. die");
        }
    }

    protected void extractFrench(String line, DefinitionList listdef){
        if(line.contains("id=\"Pronunciation")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("id=\"Pronunciation"));
                    line2 = line2.substring(0, line2.indexOf("<h"));
                    extractPronunciation(line2, listdef);
        }
        int[] indexVector = new int[_types.size()*2];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
        int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains("id=\""+_types.get(i))){
                index += line2.indexOf("id=\""+_types.get(i));
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verb"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                if(line2.contains("id=\"Noun")){
                    extractGenderFrench(line2, listdef);
                }
                line2 = line2.substring(line2.indexOf("<ol>"), line2.indexOf("</ol>")+5);
                extractDefinitions(line2, listdef);
            }
        }
        if(listdef.isPlural()){
            listdef.setGender("les");
        }
    }

    protected boolean extractDefinitionList(BufferedReader input, DefinitionList listdef, int lang) {
        
        boolean end = false;
        boolean find = false;
        String languaje = Language.LanStr(0, lang);
        
        String line;
        
        try {
        	System.out.println("--------------------------------------");
        	System.out.println("we are "+languaje+" to english");
        	System.out.println("--------------------------------------");

            while (!end && (line = input.readLine()) != null) {
            	if(line.contains("id=\""+languaje+"\""))
            	{
            		System.out.println("English>>>"+line+"\n");
                        String line2 = "";
                        line = input.readLine();
                        while(line != null && !line.startsWith("<h2>")){
                            line2 += line;
                            line = input.readLine();
                        }
                        end = true;
                        find = true;
                        switch(lang){
                            case 0:
                                extractEnglish(line2, listdef);
                                break;
                            case 1:
                                extractSpanish(line2, listdef);
                                break;
                            case 2:
                                extractGerman(line2, listdef);
                                break;
                            case 3:
                                extractFrench(line2, listdef);
                                break;
                            default:
                                find = false;
                        }
            	}               
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return find;
    }
}

